*     *****************************************************************   *;
*     *****************************************************************   *; 
*   Date:          December 27, 2021                               	      *;
*   Authors:       Lisa Dellmuth and Jonas Tallberg				          *;   
*   Purpose: 	   Analyses for book Legitimacy Politics: Elite  		  *;
*    Communication and Public Opinion in Global Governance (CUP, 2023)    *;
*     ****************************************************************    *;
*     ****************************************************************    *;

***Appendix T

*balance tests
use purpose_de_pooled.dta, clear
edu
age
know
confgov
intcoop
lr
trust
media

regress edu i.purposeR2 i.power ///
i.transR i.partR i.fairR i.problemR [aweight=weight], vce(cluster id)
eststo edu
regress Age i.purposeR2 i.power ///
i.transR i.partR i.fairR i.problemR [aweight=weight], vce(cluster id)
eststo Age
regress know_correct_index i.purposeR2 i.power ///
i.transR i.partR i.fairR i.problemR [aweight=weight], vce(cluster id)
eststo know_correct_index
regress confgov i.purposeR2 i.power ///
i.transR i.partR i.fairR i.problemR [aweight=weight], vce(cluster id)
eststo confgov
regress intcoop i.purposeR2 i.power ///
i.transR i.partR i.fairR i.problemR [aweight=weight], vce(cluster id)
eststo intcoop
regress lr i.purposeR2 i.power ///
i.transR i.partR i.fairR i.problemR [aweight=weight], vce(cluster id)
eststo lr
regress trust i.purposeR2 i.power ///
i.transR i.partR i.fairR i.problemR [aweight=weight], vce(cluster id)
eststo trust
regress media i.purposeR2 i.power ///
i.transR i.partR i.fairR i.problemR [aweight=weight], vce(cluster id)
eststo media



#delimit ;
esttab edu Age know_correct_index confgov intcoop lr trust media using balance_us.rtf,
starlevels(* .05 ** .01 *** .001) b(%9.3f) ar2(%9.3f) nodepvars scalars(F)
se(%9.2f) replace;
#delimit cr




***dichotomous DV

tab confdum
logit confdum i.purposeR2 i.power ///
i.transR i.partR i.fairR i.problemR [iweight=weight] if attentive==1, vce(cluster id) 
estimates store conf_cont_de_dummy
margins, dydx(*) vce(uncond) post // post stores results in e() for the coefplot below. run directly after
*predict p if e(sample)

#delimit ;
coefplot(conf_cont_de_dummy), headings(1.purposeR2="{bf:Social Purpose}" 
1.power="{bf:Authority}" 1.transR="{bf:Transparency}" 1.partR="{bf:Participation}"
1.fairR="{bf:Fair Outcome}" 1.problemR="{bf:Problem-solving}") 
xline(0) drop(_cons) title("Germany") fxsize(60) base 
saving(conf_cont_de_dummy.gph, replace);
#delimit cr


graph combine conf_cont_us_dummy.gph conf_cont_de_dummy.gph




*interactions

use purpose_de_pooled.dta, clear

regress conf_cont i.purposeR2##i.confgov i.power ///
i.transR i.partR i.fairR i.problemR [aweight=weight]  if attentive==1, vce(cluster id)
qui margins, at(confgov=(0(1)10)) vsquish vce(uncond) dydx(purposeR2) saving(margins_confgov_de, replace) 
use margins_confgov_de.dta, clear
*ADDRESS OVERLAY PROBLEM
clonevar _mx = _at
replace _mx = cond(_deriv == 3, _mx - 0.1, _mx + 0.1) 
replace _mx=_mx-0.1 if _deriv==2
replace _mx=_mx+0.2 if _deriv==3
replace _mx=_mx+0.1 if _deriv==4
replace _mx=_mx+0.2 if _deriv==5
replace _mx=_mx+0.3 if _deriv==6 
ed _deriv _margin _ci_lb _ci_ub _at _mx

*MARGINSPLOT USING TWOWAY
twoway ///
(scatter _margin _mx if _deriv==2, lpattern(solid) msymbol(C)  color(black)) /// human rights
(scatter _margin _mx if _deriv==3, lpattern(dash) msymbol(S)  color(g3)) /// public health
(scatter _margin _mx if _deriv==4, lpattern(dot) msymbol(T)  color(g6)) ///  
(scatter _margin _mx if _deriv==5, lpattern(shortdash) msymbol(Oh)  color(g9)) ///  
(scatter _margin _mx if _deriv==6, lpattern(shortdash_dot) msymbol(Dh)  color(g12)) ///  
(rcap _ci_lb _ci_ub _mx if _deriv==2, sort pstyle(ci, width(medium))  color(black)) /// human rights
(rcap _ci_lb _ci_ub _mx if _deriv==3, sort pstyle(ci, width(medium)) color(g3)) /// public health 
(rcap _ci_lb _ci_ub _mx if _deriv==4, sort pstyle(ci, width(medium)) color(g6)) ///   
(rcap _ci_lb _ci_ub _mx if _deriv==5, sort pstyle(ci, width(medium)) color(g9)) ///   
(rcap _ci_lb _ci_ub _mx if _deriv==6, sort pstyle(ci, width(medium)) color(g12)) , ///   
ytitle("Marginal effects of social purpose, with 95% CIs") ///
xtitle("Confidence in government") ///
xla(1(1)11, valuelabel) xlabel(1 `""no" "confidence""' 2 "1" 3 "2" 4 "3" 5 "4" 6 "5"  7 "6" 8 "7" 9 "8" 10 "9" 11 `""complete" "confidence""') graphregion(color(white)) ///
legend(pos(5) ring(0) cols(1) order(1 "Reduce poverty" 2 "Protect human rights" 3 "Promote free trade" 4 "Combat climate change" 5 "Promote public health")) ///
saving(int_confgov_de, replace) 

graph combine int_confgov_us.gph int_confgov_de.gph , saving(Fig5.1_confgov)
